package com.etong.frame.directive.impl.database;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class Placeholder {

	public final static String JDBCTYPE = "jdbcType";
	public final static String REQUIRED = "required";
	public final static String ISARRAY = "isArray";
	public final static String VARCHAR = "varchar";
	public final static String INTEGER = "integer";
	public final static String ENCRYPTOR = "encryptor"; // 加密

	protected static final Logger logger = LoggerFactory
			.getLogger(Placeholder.class);

	private String name;
	private String type;
	private String placeholder;
	private String encryptor;

	private boolean varchar;
	private boolean required;
	private boolean isArray;

	public boolean isArray() {
		return isArray;
	}

	public void setArray(boolean isArray) {
		this.isArray = isArray;
	}

	public boolean isRequired() {
		return required;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	public boolean isVarchar() {
		return varchar;
	}

	public void setVarchar(boolean varchar) {
		this.varchar = varchar;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String jdbcType) {
		this.type = jdbcType;
	}

	public String getPlaceholder() {
		return placeholder;
	}

	public void setPlaceholder(String placeholder) {
		this.placeholder = placeholder;
	}

	public void setProperty(String expr) {
		Pattern pattern = Pattern.compile("((\\w+) *= *(\\w+))");
		Matcher matcher = pattern.matcher(expr);

		while (matcher.find()) {
			String propName = matcher.group(2);
			String propValue = matcher.group(3);
			if (JDBCTYPE.equalsIgnoreCase(propName)) {
				setType(propValue);
				if (VARCHAR.equalsIgnoreCase(type)) {
					setVarchar(true);
				}
			} else if (REQUIRED.equalsIgnoreCase(propName)) {
				setRequired(String.valueOf(true).equalsIgnoreCase(propValue));
			} else if (ISARRAY.equalsIgnoreCase(propName)) {
				this.setArray(String.valueOf(true).equalsIgnoreCase(propValue));
			} else if (ENCRYPTOR.equalsIgnoreCase(propName)) {
				this.setEncryptor(propValue);
			}
		}
	}

	public String getEncryptor() {
		return encryptor;
	}

	public void setEncryptor(String encryptor) {
		this.encryptor = encryptor;
	}
}